package cn.stack.springframework.test;

import cn.bugstack.springframework.beans.factory.support.DefaultListableBeanFactory;
import cn.bugstack.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import cn.bugstack.springframework.context.support.ClassPathXmlApplicationContext;
import cn.stack.springframework.test.bean.UserService;
import cn.stack.springframework.test.common.MyBeanFactoryPostProcessor;
import cn.stack.springframework.test.common.MyBeanPostProcessor;
import org.junit.Test;

public class ApiTest06 {
    @Test
    public void test_BeanFactoryPostProcessorAndBeanPostProcessor(){
        //初始化 BeanFactory
        DefaultListableBeanFactory beanFactory=new DefaultListableBeanFactory();

        //读取配置文件$注册Bean
        XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions("classpath:spring.xml");
        //BeanDefinition 加载完成 & Bean 实例化之前,修改 BeanDefinition 的属性值
        MyBeanFactoryPostProcessor beanFactoryPostProcessor = new MyBeanFactoryPostProcessor();
        beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);

        //Bean 实例化之后,修改 Bean 属性信息
        MyBeanPostProcessor beanPostProcessor = new MyBeanPostProcessor();
        beanFactory.addBeanPostProcessor(beanPostProcessor);
        //获取Bean 对象调用方法
        UserService userService = beanFactory.getBean("userService", UserService.class);
        String result = userService.queryUserInfo();
        System.out.println("测试结果: "+result);

    }

    @Test
    public void test_xml() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:springPostProcessor.xml");
        UserService userService=applicationContext.getBean("userService",UserService.class);
        String result = userService.queryUserInfo();
        System.out.println("测试结果: "+result);
    }


}
